(*
     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is CrossFPC.

     The Initial Developer of the Original Code is Simon Kissel.
     Portions created by Benjamin Rosseaux are Copyright (C) Benjamin Rosseaux.
     All Rights Reserved.

*)

unit cfdirectoryedit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TDirectoryEdit = class(TForm)
    GroupBox1: TGroupBox;
    ListBox1: TListBox;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Button3Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
    FTarget: TEdit;
  public
    { Public declarations }
    procedure StartEdit(const Target: TEdit; Title: string);
  end;

var
  DirectoryEdit: TDirectoryEdit;

implementation

uses
  cfoptionsdialog;
{$R *.dfm}

{ TForm2 }

procedure TDirectoryEdit.StartEdit(const Target: TEdit; Title: string);
var
  s: string;
begin
  FTarget := Target;
  s := FTarget.Text;

  Listbox1.Items.Clear;

  while trim(s)<>'' do
  begin
    if pos(';',s)>0 then
    begin
      Listbox1.Items.Add(trim(Copy(s, 1, pos(';', s)-1)));
      Delete(s, 1, pos(';', s));
    end else
    begin
      Listbox1.Items.Add(trim(s));
      Break;
    end;
  end;

  Caption := Title;
  Groupbox1.Caption := 'Ordered list of '+Title;
  Edit1.Text := '';
  if Listbox1.Items.Count>0 then
  Listbox1.ItemIndex := 0;
  Listbox1.OnClick(nil);
  
  ShowModal;
end;

procedure TDirectoryEdit.Button6Click(Sender: TObject);
begin
  Close;
end;

procedure TDirectoryEdit.Button7Click(Sender: TObject);
var
  s: string;
  i: Integer;
begin
  s := '';
  for i:=0 to Listbox1.Items.Count-1 do
  begin
    if i<>0 then s := s + ';';
    s := s + Listbox1.Items[i];
  end;
  FTarget.Text := s;
  Close;
end;

procedure TDirectoryEdit.Edit1Change(Sender: TObject);
var
  entry_exists: Boolean;
begin
  entry_exists := Listbox1.Items.IndexOf(Edit1.Text)<>-1;
  Button1.Enabled := not entry_exists;
  Button2.Enabled := not entry_exists;
end;

procedure TDirectoryEdit.ListBox1Click(Sender: TObject);
begin
  if Listbox1.ItemIndex <> -1 then
    Edit1.Text := Listbox1.Items[Listbox1.ItemIndex];
end;

procedure TDirectoryEdit.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Button2.Enabled then
      Button2.Click;

    Key := #0;
  end;
  
end;

procedure TDirectoryEdit.Button3Click(Sender: TObject);
begin
  if Listbox1.ItemIndex <> -1 then
    Listbox1.Items.Delete(Listbox1.ItemIndex);
end;

procedure TDirectoryEdit.SpeedButton2Click(Sender: TObject);
var
  s: string;
  i: Integer;
begin
  with Listbox1 do
  if ItemIndex>0 then
  begin
    i := ItemIndex;
    s := Items[i-1];
    Items[i-1] := Items[i];
    Items[i] := s;
    ItemIndex := i - 1;
  end;
end;

procedure TDirectoryEdit.SpeedButton1Click(Sender: TObject);
var
  s: string;
  i: Integer;
begin
  with Listbox1 do
  if ItemIndex<Items.Count-1 then
  begin
    i := ItemIndex;
    s := Items[i+1];
    Items[i+1] := Items[i];
    Items[i] := s;
    ItemIndex := i + 1;
  end;
end;

procedure TDirectoryEdit.Button1Click(Sender: TObject);
begin
  with Listbox1 do
  if ItemIndex <> -1 then
  begin
    Items[ItemIndex] := Edit1.Text;
    Edit1.Text := '';
  end;
end;

procedure TDirectoryEdit.Button2Click(Sender: TObject);
begin
  Listbox1.Items.Add(Edit1.Text);
  Edit1.Text := '';
end;

procedure TDirectoryEdit.Button4Click(Sender: TObject);
var
  i: Integer;
begin
  i := 0;
  while i < Listbox1.Items.Count do
  begin
    if DirectoryExists(Listbox1.Items[i]) then
      inc(i)
    else
      Listbox1.Items.Delete(i);
  end;
end;

procedure TDirectoryEdit.Button5Click(Sender: TObject);
var
  s: string;
begin
  s:=SearchFolder(application.handle, 'Browse for CrossFPC directory',false);
  if s <> '' then
    Edit1.Text := s;
end;

end.
